<template>
  <div class="login-container">
    <el-card class="login-card" shadow="always">
      <template #header>
        <div class="login-header">
          <h1 class="login-title">ChatApp</h1>
          <p class="login-subtitle">欢迎回来</p>
        </div>
      </template>
      
      <el-form
        ref="loginFormRef"
        :model="form"
        :rules="rules"
        label-position="top"
        size="large"
        @submit.prevent="handleLogin"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="form.username"
            placeholder="请输入用户名"
            :prefix-icon="User"
            :disabled="user.loading"
            clearable
          />
        </el-form-item>
        
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="form.password"
            type="password"
            placeholder="请输入密码"
            :prefix-icon="Lock"
            :disabled="user.loading"
            show-password
            @keyup.enter="handleLogin"
          />
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="primary"
            size="large"
            :loading="user.loading"
            @click="handleLogin"
            class="login-button"
          >
            {{ user.loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
      </el-form>
      
      <!-- 快速操作 -->
      <div class="quick-actions">
        <el-divider>或者</el-divider>
        <el-button
          type="primary"
          plain
          size="large"
          :icon="User"
          @click="goToRegister"
          class="register-button"
        >
          注册新用户
        </el-button>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock, Plus } from '@element-plus/icons-vue'
import { useUserStore } from '../stores/user'
import type { FormInstance, FormRules } from 'element-plus'

const router = useRouter()
const user = useUserStore()
const loginFormRef = ref<FormInstance>()

const form = reactive({
  username: '',
  password: ''
})

// 表单验证规则
const rules: FormRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 2, max: 20, message: '用户名长度在 2 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  try {
    await loginFormRef.value.validate()
    
    const result = await user.login({
      username: form.username.trim(),
      password: form.password.trim()
    })
    
    if (result.success) {
       ElMessage.success('登录成功！')
       router.push('/app/character-square')
     } else {
      ElMessage.error(user.error || '登录失败，请重试')
    }
  } catch (error) {
    console.error('表单验证失败:', error)
  }
}



const goToRegister = () => {
  router.push('/register')
}
// 组件挂载时初始化用户信息
onMounted(() => {
  user.initFromStorage()
  // 如果已经登录，直接跳转到角色广场
  if (user.isLoggedIn) {
    router.push('/app/character-square')
  }
})
</script>

<style scoped>
.login-container {
  position: fixed;
  inset: 0; /* 覆盖整个视窗 */
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #fdf2f8 0%, #fce7f3 50%, #f9a8d4 100%);
  padding: 20px;
  overflow: auto; /* 内容超出时可滚动 */
}

.login-card {
  width: 100%;
  max-width: 420px;
  border-radius: 16px;
  overflow: hidden;
}

.login-header {
  text-align: center;
  padding: 10px 0;
}

.login-title {
  font-size: 32px;
  font-weight: bold;
  color: #ec4899;
  margin: 0 0 8px 0;
  text-shadow: 0 2px 4px rgba(236, 72, 153, 0.1);
}

.login-subtitle {
  font-size: 16px;
  color: #be185d;
  margin: 0;
  opacity: 0.8;
}

.login-button {
  width: 100%;
  height: 48px;
  font-size: 16px;
  font-weight: 600;
  background: linear-gradient(135deg, #ec4899, #db2777);
  border: none;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.login-button:hover {
  background: linear-gradient(135deg, #db2777, #be185d);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(236, 72, 153, 0.3);
}



.demo-alert p {
  margin: 4px 0;
  font-size: 14px;
}

.quick-actions {
  margin-top: 20px;
  text-align: center;
}

.create-button {
  width: 100%;
  height: 44px;
  font-size: 15px;
  border-radius: 8px;
  border-color: #10b981;
  color: #10b981;
  transition: all 0.3s ease;
}

.create-button:hover {
  background-color: #10b981;
  color: white;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(16, 185, 129, 0.2);
}

/* Element Plus 组件样式覆盖 */
:deep(.el-card__header) {
  background: linear-gradient(135deg, #fdf2f8, #fce7f3);
  border-bottom: 1px solid #f9a8d4;
}

:deep(.el-form-item__label) {
  color: #ec4899;
  font-weight: 600;
  font-size: 14px;
}

:deep(.el-input__wrapper) {
  border-radius: 8px;
  box-shadow: 0 0 0 1px #f9a8d4;
  transition: all 0.3s ease;
}

:deep(.el-input__wrapper:hover) {
  box-shadow: 0 0 0 1px #ec4899;
}

:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 2px #ec4899;
}

:deep(.el-divider__text) {
  color: #be185d;
  font-size: 14px;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-container {
    padding: 10px;
  }
  
  .login-card {
    max-width: 100%;
  }
  
  .login-title {
    font-size: 28px;
  }
  
  .login-subtitle {
    font-size: 14px;
  }
}
</style>